<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加学生 | 学生管理系统 | 菜鸟基地</title>
</head>

<body>
    <h1>添加学生</h1>
    <div>
        <div>
            <label for="name">姓名：</label>
            <input type="text" id="name">
        </div>
        <div>
            <label for="sex">性别：</label>
            <select id="sex">
                <option value="0">未知</option>
                <option value="1">男</option>
                <option value="2">女</option>
            </select>
        </div>
        <div>
            <label for="age">年龄：</label>
            <input type="number" id="age">
        </div>
        <div>
            <label for="no">学号：</label>
            <input type="text" id="no">
        </div>
        <div>
            <label for="class_name">班级：</label>
            <select id="class_name">
                <option value="cnjd-c1">初中班（c1）</option>
                <option value="cnjd-c2">高中班（c2）</option>
            </select>
        </div>
        <div>
            <button id="submit">提交</button>
        </div>
    </div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
<script>
    $(function () {
        $('#submit').click(function () {
            var $name = $('#name')
                , $sex = $('#sex')
                , $age = $('#age')
                , $no = $('#no')
                , $class_name = $('#class_name');
            var name = $.trim($name.val());
            var sex = $sex.val();
            var age = $.trim($age.val());
            var no = $.trim($no.val());
            var class_name = $class_name.val();
            if (name === '') {
                alert('请输入姓名');
            } else if (!/^\d{1,3}$/.test(age)) {
                alert('年龄非法，请重新输入');
            } else if (no === '') {
                alert('请输入学号');
            } else {
                $.post('/api/student/insert', {
                    name: name,
                    sex: sex,
                    age: age,
                    no: no,
                    class_name: class_name
                }, function (res) {
                    alert(res.msg);
                    if (res.code === 1) {
                        window.location.href = '/students';
                    }
                });
            }
        });
    });
</script>

</html>